//
//  CYNSearchBar.swift
//  CYN微博
//
//  Created by chendada_ on 16/5/19.
//  Copyright © 2016年 chendada. All rights reserved.
//

import UIKit

class CYNSearchBar: UIView {

    @IBOutlet weak var searchTF: UITextField!
   
    @IBOutlet weak var rightCons: NSLayoutConstraint!

    @IBOutlet weak var cancelBtn: UIButton!
    //加载xib
   class func loadSearchBar() -> CYNSearchBar {
        let searchView = NSBundle.mainBundle().loadNibNamed("CYNSearchBar", owner: nil, options: nil).last as! CYNSearchBar
        
        return searchView
    }
    
    override func awakeFromNib() {
//        self.bounds.size.width = UIScreen.mainScreen().bounds.width
        
        //宽度
        self.bounds.size.width = ScreenWidth
        //文字输入框 有leftView
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置显示模式
        searchTF.leftViewMode = .Always
        
        //修改frame
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        //设置视图的显示模式
        searchTF.leftView?.contentMode = .Center
        
        //设置圆角
        self.layer.cornerRadius = 20
        self.layer.masksToBounds = true
        //设置边线的颜色
        self.layer.borderColor = UIColor.orangeColor().CGColor
        self.layer.borderWidth = 2
    
    }
    
    @IBAction func textFiledBeginEditing(sender: AnyObject) {
        UIView.animateWithDuration(0.25) {
            self.rightCons.constant = self.cancelBtn.bounds.width
            //提前强制刷新视图
            self.layoutIfNeeded()
        }
      
    } 
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
        //收集约束的变化
        self.rightCons.constant = 0
        UIView.animateWithDuration(0.25) {
          
            //提前强制刷新视图
            self.layoutIfNeeded()
        }
        //结束编辑状态
        self.endEditing(true)
    }
   
    
    
    
}
